package com.thinksns.sociax.t4.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.function.FunctionChangeFollow;
import com.thinksns.sociax.t4.component.GlideCircleTransform;
import com.thinksns.sociax.t4.model.ModelUser;
import com.thinksns.sociax.t4.unit.UnitSociax;

import java.util.ArrayList;
import java.util.List;

import static com.thinksns.sociax.api.Api.mContext;

/**
 * Created by zhiyicx on 2017/5/16.
 */

public class AdapterEventUserList extends BaseAdapter {

    private Context context;
    private List<ModelUser> users;
    private boolean noData;
    private UnitSociax mUnitSociax;

    public AdapterEventUserList(Context context, List<ModelUser> users) {
        this.context = context;
        this.users = users;
        if (users == null) {
            this.users = new ArrayList<>();
        }
        mUnitSociax = new UnitSociax(context);
    }

    public void resetData(List<ModelUser> users) {
        this.users.clear();
        addData(users);
    }

    public void addData(List<ModelUser> users) {
        this.users.addAll(users);
        notifyDataSetChanged();
    }

    public void setNoData() {
        this.users.clear();
        this.users.add(new ModelUser());
        this.noData = true;
    }

    public void setHasData() {
        if (noData)
            this.users.clear();
        this.noData = false;
    }

    public boolean getNoData() {
        return noData;
    }

    @Override
    public int getCount() {
        return users.size();
    }

    @Override
    public ModelUser getItem(int position) {
        return users.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (noData) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_no_data, null);

            TextView noDataView = (TextView) convertView.findViewById(R.id.item_no_data);
            ViewGroup.LayoutParams layoutParams = noDataView.getLayoutParams();
            layoutParams.height = mContext.getResources().getDisplayMetrics().heightPixels -
                    UnitSociax.dip2px(mContext, 150);
            layoutParams.width = mContext.getResources().getDisplayMetrics().widthPixels;
            noDataView.setLayoutParams(layoutParams);
            noDataView.setText("暂无成员");

            return convertView;
        }

        UserViewHolder viewHolder = null;
        if (convertView == null || convertView.findViewById(R.id.item_no_data) != null) {
            viewHolder = new UserViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.listitem_user, null);
            viewHolder.tv_user_photo = (ImageView) convertView
                    .findViewById(R.id.image_photo);
            viewHolder.tv_user_name = (TextView) convertView
                    .findViewById(R.id.unnames);
            viewHolder.tv_user_content = (TextView) convertView
                    .findViewById(R.id.uncontent);
            viewHolder.tv_user_add = (TextView) convertView
                    .findViewById(R.id.image_add);
            viewHolder.ll_userGroup = (LinearLayout) convertView.findViewById(R.id.ll_uname_adn);
            convertView.setTag(R.id.tag_viewholder, viewHolder);
        } else {
            viewHolder = (UserViewHolder) convertView.getTag(R.id.tag_viewholder);
        }

        ModelUser modelUser = getItem(position);
        viewHolder.tv_user_add.setTag(R.id.tag_position, position);
        viewHolder.tv_user_add.setTag(R.id.tag_follow, getItem(position));
        /*增加认证图标显示 8.4.2017*/
        viewHolder.ll_userGroup.removeAllViews();
        if (modelUser.getUserApprove() != null && modelUser.getUserApprove().getApprove() != null){
            mUnitSociax.addUserGroup(modelUser.getUserApprove().getApprove(), viewHolder.ll_userGroup);
        }

        Glide.with(context).load(getItem(position).getUserface())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideCircleTransform(context))
                .crossFade()
                .error(R.drawable.default_user)
                .placeholder(R.drawable.default_user)
                .into(viewHolder.tv_user_photo);

        viewHolder.tv_user_name.setText(getItem(position).getUserName());

        String intro = getItem(position).getIntro();
        if (intro == null || intro.isEmpty() || intro.equals("null"))
            intro = "这家伙很懒，什么也没留下";
        viewHolder.tv_user_content.setText(intro);

        if (!getItem(position).isFollowed()) {
            //加关注
            viewHolder.tv_user_add.setBackgroundResource(R.drawable.roundbackground_green_digg);
            viewHolder.tv_user_add.setText(R.string.fav_add_follow);
            viewHolder.tv_user_add.setTextColor(context.getResources().getColor(R.color.fav_border));
        } else {
            //取消关注
            viewHolder.tv_user_add.setBackgroundResource(R.drawable.roundbackground_fav_true);
            viewHolder.tv_user_add.setText(R.string.fav_followed);
            viewHolder.tv_user_add.setTextColor(context.getResources().getColor(R.color.fav_text_true));
        }

        /**
         * 屏蔽自己关注自己
         */
        if (getItem(position).getUid() == Thinksns.getMy().getUid())
            viewHolder.tv_user_add.setVisibility(View.GONE);
        else
            viewHolder.tv_user_add.setVisibility(View.VISIBLE);

        viewHolder.tv_user_add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                v.setClickable(false);
                FunctionChangeFollow fcChangeFollow = new FunctionChangeFollow(context,
                        AdapterEventUserList.this, v);
                fcChangeFollow.changeListFollow();
            }
        });
        return convertView;
    }

    public class UserViewHolder {
        public ImageView tv_user_photo;
        public TextView tv_user_name;
        public TextView tv_user_content;
        public TextView tv_user_add;
        public LinearLayout ll_userGroup;
    }
}
